package com.ssyc.gsk_teacher.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ssyc.common.base.Constants;
import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.activity.TeacherTakeLineListActivity;
import com.ssyc.gsk_teacher.adapter.EmpircalRvAdapter;
import com.ssyc.gsk_teacher.bean.PxbInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 经验值
 * Created by Administrator on 2018/7/4 0004.
 */

public class TeacherEmpiricalValueFragment extends LazyBaseFragment implements SrfManager.OnPullRefreshListener {

    private RecyclerView rv;
    private SmartRefreshLayout srf;
    private RelativeLayout rlLoading;
    private RelativeLayout rlContent;

    //数据集合
    private List<PxbInfo.DataBean.LISTBean> oldDatas;

    //空视图
    private View emptyView;

    //适配器
    private EmpircalRvAdapter adapter;

    //班级号
    private String classNum;

    //单体模式
    private static class LazyHolder {
        private static final TeacherEmpiricalValueFragment INSTANCE = new TeacherEmpiricalValueFragment();
    }

    public static final TeacherEmpiricalValueFragment newInstance(String classnum) {
        TeacherEmpiricalValueFragment teacherEmpiricalValueFragment = LazyHolder.INSTANCE;
        Bundle bundle = new Bundle();
        bundle.putString("classnum", classnum);
        teacherEmpiricalValueFragment.setArguments(bundle);
        return teacherEmpiricalValueFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.teacher_fragment_empirical_value;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {
    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);
        showContent();

        //初始化视图
        initView(view);

        //获取班级号
        initIntent();

        //初始化下拉刷新控件
        initSrf();

        //设置数据
        initRv();

        //请求数据
        http(BqaManager.NORML);
    }

    /**
     * 请求服务器获取数据
     *
     * @param method
     */
    private void http(int method) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "48");
        map.put("acc", AccountUtils.getAccount(getContext()));
        map.put("role", AccountUtils.getRole(getContext()));
        map.put("apptoken", AccountUtils.getTestToken(getContext()));
        map.put("classNum", classNum);
        map.put("platform", "8");

        HttpUtils.post(HttpAdress.PXB_THREE, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                BqaManager.setErrorNet(rlLoading, srf, method, adapter);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }

                if (rlContent != null && rlContent.getVisibility() != View.VISIBLE) {
                    rlContent.setVisibility(View.VISIBLE);
                }

                if (!TextUtils.isEmpty(response)) {
                    PxbInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, PxbInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
                            List<PxbInfo.DataBean> dataBeans = info.getData();
                            if (dataBeans != null && dataBeans.size() == 3) {
                                PxbInfo.DataBean dataBean = dataBeans.get(1);
                                if (dataBean != null) {
                                    List<PxbInfo.DataBean.LISTBean> newDatas = dataBean.getLIST();
                                    if (newDatas != null) {
                                        if (method == BqaManager.NORML) {
                                            oldDatas.clear();
                                            oldDatas.addAll(newDatas);
                                            BqaManager.setRv(emptyView, getContext(), adapter, rv);
                                        } else {
                                            //更新数据
                                            BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                                            adapter.setEnableLoadMore(false);
                                        }
                                    }
                                }
                            }
                        } else {
                            UiUtils.Toast(Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }
                    } else {
                        Log.i("test", Constants.EMPTY);
                        if (method == BqaManager.NORML) {
                            BqaManager.setRv(emptyView, getContext(), adapter, rv);
                        }else{
                            if (srf != null) {
                                srf.finishRefresh();
                            }
                            oldDatas.clear();
                            adapter.notifyDataSetChanged();
                            adapter.setEnableLoadMore(false);
                        }
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }

    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        rv = view.findViewById(R.id.rv);
        srf = view.findViewById(R.id.srf);
        rlLoading = view.findViewById(R.id.rl_loading);
        rlContent = view.findViewById(R.id.rl_conent);
        rlLoading.setVisibility(View.VISIBLE);
        rlContent.setVisibility(View.GONE);
    }


    /**
     * 获取班级号
     */
    private void initIntent() {
        classNum = getArguments().getString("classnum");
    }

    /**
     * 初始化下拉刷新
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
        srf.setPrimaryColors(getContext().getResources().getColor(R.color.transparent), getContext().getResources().getColor(R.color.white));
    }


    /**
     * 设置列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(getContext(), R.layout.base_rv_common_empty_view, null);
        adapter = new EmpircalRvAdapter(getActivity(), getContext(), R.layout.teacher_rv_empircal, oldDatas);
    }


    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }

    /**
     * 选择班级后刷新数据
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
        if (busInfo != null && TeacherTakeLineListActivity.UPDATE_REFRESH == busInfo.getType()) {
            classNum = busInfo.getClassNum();
            http(BqaManager.PULLREFRESH);
        }
    }
}
